package com.tuanzi.loan.web.bean.galaxycard;

import javax.annotation.PostConstruct;
import javax.faces.bean.ViewScoped;

import lombok.Data;
import lombok.EqualsAndHashCode;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.tuanzi.loan.business.entity.galaxycard.GalaxyCardPromoteUserChannel;
import com.tuanzi.loan.business.service.galaxycard.GalaxyCardPromoteUserRelateChannelService;
import com.tuanzi.loan.core.aspect.Debug;
import com.tuanzi.loan.vo.request.galaxycard.GalaxyCardPromoteUserRelateChannelRequest;
import com.tuanzi.loan.vo.response.galaxycard.GalaxyCardPromoteUserRelateChannelResponse;
import com.tuanzi.loan.web.configuration.JSFListingBean;
import com.tuanzi.loan.web.lazy.galaxycard.GalaxyCardPromoteUserRelateChannelLazyDataModel;

/**
 * 银河黑卡外推用户渠道关联管理Bean
 * @author chenweijun 2018/02/01
 *
 */
@Data
@Debug
@Component
@ViewScoped
@EqualsAndHashCode(callSuper = true)
public class GalaxyCardPromoteUserRelateChannelBean extends JSFListingBean<GalaxyCardPromoteUserRelateChannelRequest, GalaxyCardPromoteUserRelateChannelResponse> {
	
	
	private static final long serialVersionUID = 6940954991406168674L;
	
	@Autowired
	private GalaxyCardPromoteUserRelateChannelService galaxyCardPromoteUserRelateChannelService;
	
	private String userId;
	
	@PostConstruct
	private void init() {
		userId = getParamFromRequest("userId");
		initLazyDataModel();
	}
	
	private void initLazyDataModel() {
		$ = new GalaxyCardPromoteUserRelateChannelRequest();
		$.setUserId(userId);
		lazyDataModel = new GalaxyCardPromoteUserRelateChannelLazyDataModel($);
	}
	
	public void onRelateChannel(Integer channelCode){
		
		GalaxyCardPromoteUserChannel galaxyCardPromoteUserChannel = new GalaxyCardPromoteUserChannel();
		galaxyCardPromoteUserChannel.setUserId(userId);
		galaxyCardPromoteUserChannel.setChannelCode(channelCode);
		galaxyCardPromoteUserRelateChannelService.saveOrUpdate(galaxyCardPromoteUserChannel);
	}
	
	public void onUnrelateChannel(Integer channelCode){
		GalaxyCardPromoteUserChannel galaxyCardPromoteUserChannel = galaxyCardPromoteUserRelateChannelService.findByUserIdAndChannelCode(
				userId, channelCode);
		galaxyCardPromoteUserRelateChannelService.delete(galaxyCardPromoteUserChannel);
	}

}
